package com.camp.android.trip.n.track;

import java.util.Calendar;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;

import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;

import android.widget.DatePicker;

import android.widget.TextView;
import android.widget.TimePicker;
import com.camp.android.trip.n.track.R;

public class NewItemActivity extends Activity {
	public int tripid;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new_item);
        getActionBar().setDisplayHomeAsUpEnabled(true);
        Intent intent = getIntent();
        tripid = Integer.parseInt(intent.getStringExtra("trip_id"));
        
    } 

    @Override
    protected void onStart() {
    	super.onStart();
    	Intent intent = getIntent();
    	if(intent.getStringExtra("item_name") != null)
    		((TextView)findViewById(R.id.item_name_text)).setText(intent.getStringExtra("item_name"));
        tripid = Integer.parseInt(intent.getStringExtra("trip_id"));
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_new_item, menu);
        return true;
    }

    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                NavUtils.navigateUpFromSameTask(this);
                return true;
        }
        return super.onOptionsItemSelected(item);
    }
    
    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {

        View v = getCurrentFocus();
        boolean ret = super.dispatchTouchEvent(event);

        if (v instanceof EditText) {
            View w = getCurrentFocus();
            int scrcoords[] = new int[2];
            w.getLocationOnScreen(scrcoords);
            float x = event.getRawX() + w.getLeft() - scrcoords[0];
            float y = event.getRawY() + w.getTop() - scrcoords[1];

            Log.d("Activity", "Touch event "+event.getRawX()+","+event.getRawY()+" "+x+","+y+" rect "+w.getLeft()+","+w.getTop()+","+w.getRight()+","+w.getBottom()+" coords "+scrcoords[0]+","+scrcoords[1]);
            if (event.getAction() == MotionEvent.ACTION_UP && (x < w.getLeft() || x >= w.getRight() || y < w.getTop() || y > w.getBottom()) ) { 

                InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(getWindow().getCurrentFocus().getWindowToken(), 0);
            }
        }
    return ret;
    }
    
    public void create(View view){
    	// get user entered info
    	String name = ((TextView) findViewById(R.id.item_name_text)).getText().toString();
    	DatePicker startDate = (DatePicker) findViewById(R.id.start_date_picker);
    	TimePicker startTime = (TimePicker) findViewById(R.id.start_time_picker);
    	Calendar start = Calendar.getInstance();
    	start.set(
    			startDate.getYear(),startDate.getMonth(),startDate.getDayOfMonth(),
    			startTime.getCurrentHour(),startTime.getCurrentMinute());
    	
    	DatePicker endDate = ((DatePicker) findViewById(R.id.end_date_picker));
    	TimePicker endTime = (TimePicker) findViewById(R.id.start_time_picker);
    	Calendar end = Calendar.getInstance();
    	end.set(
    			endDate.getYear(),endDate.getMonth(),endDate.getDayOfMonth(),
    			endTime.getCurrentHour(),endTime.getCurrentMinute());;
    			
    	// create new Item object
    	Item item = new Item(name,start,end);
    	// add trip to database
    	DatabaseManager db = new DatabaseManager(this);
    	int id = db.addItem(tripid, item);
    	// open item page
    	Intent intent = new Intent(this, ItemActivity.class);
    	intent.putExtra("item_id", Integer.toString(id));
		startActivity(intent);
    }

}
